package me.youline.dataServer.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import me.youline.dataServer.controller.dto.IdPaginationDTO;
import me.youline.dataServer.controller.dto.PubMsgBaseFrontDTO;
import me.youline.dataServer.entity.PublicMessageIndex;
import me.youline.dataServer.service.front.IdPageDTOAdapter;
import me.youline.dataServer.service.front.PublicMessageDTOAdapter;
import me.youline.dataServer.service.pubMsg.PublicMessageFacade;
import me.youline.dataServer.service.support.MsgBuilder;
import me.youline.dataServer.utils.ResponseTypeOutputUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/visitor/")
public class VisitorController {

	private Logger logger = LoggerFactory
			.getLogger(PublicMessageController.class);
	
	@Autowired
	private PublicMessageFacade publicMessageFacade;
	
	@Autowired
	private PublicMessageDTOAdapter msgAssembler;
	
	/**
	 * 获取广场列表数据
	 * 
	 * @param token
	 * @param idPaginationDTO
	 * @param res
	 */
	@RequestMapping(value = "/square/list", method = RequestMethod.GET)
	public void getSquareAreaMsg(@ModelAttribute IdPaginationDTO idPaginationDTO,HttpServletResponse res) {
		try {
			List<PublicMessageIndex> pubMsgs = publicMessageFacade
					.getSquareMsgList(IdPageDTOAdapter.getPagination(idPaginationDTO));
			List<PubMsgBaseFrontDTO> msgDTOList = msgAssembler.getPubMsgBaseFrontDTOs(pubMsgs);
			res.setStatus(HttpServletResponse.SC_OK);
			ResponseTypeOutputUtils.renderJson(res, MsgBuilder.success().addObj("pubList", msgDTOList).toJSONString());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			ResponseTypeOutputUtils.renderJson(res, MsgBuilder.error()
					.errorCode("100001").errorMsg("游客获取广场列表业务服务端异常")
					.toJSONString());
			return;
		}
	}
}
